package com.moleq.adapter;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.moleq.mgpos.activities.UserSettingActivity;
import com.moleq.models.Functions;
import com.moleq.mgpos.activities.R;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class FunctionListAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private List<Functions> listItem,listItem_user;
	private int selectedRow = 0;
	public boolean lb_check;
	public CheckBox cb_check;
	public UserSettingActivity usa;
	
	public FunctionListAdapter(Context context,List<Functions> listItem,List<Functions> listItem_user){
		this.mInflater = LayoutInflater.from(context);
		this.listItem = listItem;
		this.listItem_user = listItem_user;
		usa  = (UserSettingActivity)context;
		ischeck();
	}
	
	public void setSource(List<Functions> listItem,List<Functions> listItem_user){
		this.listItem = listItem;
		this.listItem_user = listItem_user;
		ischeck();
	}
	
	public int getCount() {
		// TODO Auto-generated method stub
		return listItem.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listItem.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		// TODO Auto-generated method stub
				ViewHolder holder = null;
//				if(convertView == null){
					holder = new ViewHolder();
					convertView = mInflater.inflate(R.layout.permissions_list_item, null);
					holder.fs_name  = (TextView) convertView.findViewById(R.id.tv_permissions_name);
					holder.cb_check = (CheckBox) convertView.findViewById(R.id.cb_check);
//					convertView.setTag(holder); 
//				}else{
//					holder = (ViewHolder) convertView.getTag();
//				}
				holder.fs_name.setTextSize(16);

				String ls_fs_name = String.valueOf(listItem.get(position).getFs_name());
				holder.fs_name.setText(ls_fs_name);

				holder.fs_name.setBackgroundColor(Color.rgb(204, 204, 204));
				holder.cb_check.setBackgroundColor(Color.rgb(204, 204, 204));
				
				if (listItem.get(position).getIscheck()) {
					holder.cb_check.setChecked(true);
				}
				
				holder.cb_check.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
					@Override  
					public void onCheckedChanged(CompoundButton buttonView,  
					boolean isChecked) {  
					// TODO Auto-generated method stub  
					usa.SetEnabledFalse();
					listItem.get(position).setIscheck(isChecked);
					}  
				});  
				
				return convertView;
	}
	
	public OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener(){

		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			// TODO Auto-generated method stub
			lb_check = isChecked;
			usa.SetEnabledFalse();
		}
	};

	public void ischeck(){
		
		for (int j = 0; j < listItem.size(); j++) {
			listItem.get(j).setIscheck(false);
			String ls_fs_name = String.valueOf(listItem.get(j).getFs_name());
			for (int i = 0; i < listItem_user.size(); i++) {
				String ls_name = String.valueOf(listItem_user.get(i).getFs_name());
				
				if(ls_fs_name.equals(ls_name)){						
					listItem.get(j).setIscheck(true);
				}
				
			}
		}
	}
	
	public final class ViewHolder{
		public TextView fs_name ;
		public CheckBox cb_check;

	}
	public void ScrollToRow(int row) {
		selectedRow = row;
		this.notifyDataSetChanged();
	}
	public int GetSelectedIndex(){
		return selectedRow;
	}

};
